$(document).ready(function(){
//	获取url参数  per_id传过来就变成了json字符串
	var role_id = getQueryString("role_id");
	var per_id = decodeURIComponent(getQueryString("per_id"));
	var rolename = getQueryString("rolename");
	console.log(per_id);
	console.log(rolename);
	$("#rolename").text(decodeURIComponent(rolename));
	var per_id_array=per_id.split(",");
	console.log("per_id[0]:"+per_id_array[0]);
	//	等待进度圈
	$(document).ajaxStart(
			function(){$.blockUI({
				message:$('#loading'),
				 css: {
                     border: 'none',                   // 无边界
                     width:"20px",                     // 中间框框的宽度
                     top:"50%",                        // 高居中
                     left:"50%"                        // 左居中
                },
                overlayCSS:{backgroundColor:'#fff'}
						})
	}).ajaxStop($.unblockUI);
//	获取后台传来的所有per_id 权限名
	$.ajax({
		"type":"get",
		"url":"../PermissionInfoServlet?method=GetPermissionsList",
		"data":{
			"pagenum":1
		},
		success:function(data){
			console.log(data); 
			appendRolePermissionInfo(data,per_id_array,role_id,per_id_array);
		},
		error:function(){
			console.log("error");
		}
	})
})
function getQueryString(paraname) {
	var reg = new RegExp("(^|&)" + paraname + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return (r[2]);
	}
	return null;
}
function appendRolePermissionInfo(data,per_id_array,role_id,per_id_array){
	var recordsTotal = JSON.parse(data).recordsTotal;
	var PersInfo = JSON.parse(data).PersInfo;
	$.each(PersInfo,function(indexs,values){
		var flag =0;
//	    	判断该角色是否拥有该权限
		for(var i=0;i<per_id_array.length;i++){
			if(per_id_array[i]==this._id){
				console.log("=="+this._id);
				var editbtn = "<td class='center'>"
    	            +"<button id='"+this._id+"_delete' class='btn btn-danger' >"
                    +"<i class='glyphicon glyphicon-trash icon-white'></i>删除</button>"
                    flag =1;
				break;
			}
		}
		if(flag==0){
			console.log("!= "+this._id);
			var editbtn = "<td class='center'>"
	            +"<button id='"+this._id+"_add' class='btn btn-success' >"
                +"<i class='glyphicon glyphicon-zoom-in icon-white'></i>添加</button>"
		}
    	var tr = "<tr><td>"+this.permissionname+"</td><td>"
    			+this.permissiontype+"</td><td>"+this.permissioncode+"</td><td>"+this.per_detail+"</td>";
    
//	    	遍历添加行
    	var tr = tr + editbtn;
    	$("#roleChangeTable").append(tr);
//	    	绑定添加按钮点击事件
    	if(flag==0){
    		var addbtn_id =this._id+"_add";
        	$("#"+addbtn_id).click(function(){
        		console.log("addbtn_id:"+this.id);
        		var add_per_id = this.id.split("_");
        		addPermissionId(add_per_id[0],role_id,per_id_array);
        	});
    	}
    	else{
//    		绑定删除按钮点击事件
        	var deletebtn_id =this._id+"_delete";
        	$("#"+deletebtn_id).click(function(){
        		console.log("deletebtn_id:"+this.id);
        		var delete_per_id = this.id.split("_");
        		deletePermissionId(delete_per_id[0],role_id,per_id_array);
        	});
    	}
	});
	 var url ='../PermissionInfoServlet?method=GetPermissionsList';
	 pageMaker(recordsTotal,url,"pagetoolBar");
}
//为角色添加新权限
function addPermissionId(perid,role_id,per_id_array){
	$.ajax({
		"type":"get",
		"url":"../GetRolesInfo?method=AddPer",
		"data":{
			"role_id":role_id,
			"per_id":perid
		},
		success:function(data){
			console.log(data);
			per_id_array.push(perid);
			var url = "../WebManagement/editRole.jsp?role_id="+role_id+"&per_id="+per_id_array+"&rolename="+role_id;
			location.href=url;
		},
		error:function(){
			console.log("error");
		}
	});
}
//删除角色权限
function deletePermissionId(perid,role_id,per_id_array){
	$.ajax({
		"type":"get",
		"url":"../GetRolesInfo?method=RemovePer",
		"data":{
			"role_id":role_id,
			"per_id":perid
		},
		success:function(data){
			console.log(data);
			per_id_array.splice($.inArray(perid,per_id_array),1);
			var url = "../WebManagement/editRole.jsp?role_id="+role_id+"&per_id="+per_id_array+"&rolename="+role_id;
			location.href=url;
		},
		error:function(){
			console.log("error");
		}
	});
}